package com.shengqiang.zhenhuiTransfer.config;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:jdbc.properties")
@MapperScan("com.shengqiang.zhenhuiTransfer.mapper")
public class MybatisPlusConfig {

    @Autowired
    private Environment env;


    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }

    /**
     * mybatis-plus SQL执行效率插件【生产环境可以关闭】
     */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }

//    @Bean
//    public GlobalConfiguration globalConfiguration(){
//        GlobalConfiguration globalConfiguration=new GlobalConfiguration();
//        globalConfiguration.setMetaObjectHandler(new MyMetaObjectHandler());
//        return globalConfiguration;
//    }

    @Bean
    public DataSource dataSource(){
        HikariDataSource dataSource=new HikariDataSource();
        dataSource.setJdbcUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setMaximumPoolSize(Integer.parseInt(env.getProperty("spring.datasource.maximumPoolSize")));
        dataSource.setIdleTimeout(Long.parseLong(env.getProperty("spring.datasource.idleTimeout")));
        dataSource.setConnectionTimeout(Long.parseLong(env.getProperty("spring.datasource.connectionTimeout")));
        return dataSource;
    }
}
